jQuery(function()
{        
    $("#btnSearch" ).hover(
        function() {
            $(this).removeClass( "icon-search-green" ).addClass( "icon-search-white" );
        }, 
        function() {
            $(this).removeClass( "icon-search-white" ).addClass( "icon-search-green" );
        }
    ); 
    
    $(".dropdown-menu-list li").hover(
        function() {
            $(this).children("a").addClass( "maintainHover" );
            $(this).children("a").children("i").removeClass( "icon-next" ).addClass( "icon-next-white" );
        } ,
        function() {
            $(this).children("a").removeClass( "maintainHover" );
            $(this).children("a").children("i").removeClass( "icon-next-white" ).addClass( "icon-next" );
        }          
    );
    
    $("#txtSearchValue" )
    .focusout(
        function() {
            $('#btnSearch').removeClass( "icon-search-black" ).addClass( "icon-search-green" );
        }
    )  
    .focus(
        function() {
            $('#btnSearch').removeClass( "icon-search-green" ).addClass( "icon-search-black" );
        }
    ); 
    
    ReturnSearchInput();
    $("#txtSearchValue").keyup(function(e) {
        if(e.keyCode!=37&&e.keyCode!=38&&e.keyCode!=39&&e.keyCode!=40)
        {
            var searchVal=$("#txtSearchValue").val();

            if(searchVal!=='')
            {
                if(searchVal.length>=2)
                {
                    ShowSearchInput(searchVal);
                }
                else
                    ReturnSearchInput();
            }
            else
            {
                ReturnSearchInput();
            }            
        }
    });

    //Menu
    var vali=$('.dropdown-menu').attr('isshow');
    if(vali==='0')
    {
        $('.dropdown').hoverIntent(
        {
            over:function(){$('.dropdown-menu').fadeIn('fast');$(this).nextAll().addClass('halfopaque');$('#open_menu').addClass('open-menu');},
            out:function(){$('.dropdown-menu').fadeOut('fast');$(this).nextAll().removeClass('halfopaque');$('#open_menu').removeClass('open-menu');},
            timeout:400
        });
    }
    else
    {
        $('.dropdown-menu').fadeIn('2000');
        $(this).nextAll().addClass('halfopaque');
        $('#open_menu').addClass('open-menu');
        var mheight=$('.dropdown-menu').height();
        $('.filtering').css('margin-top',mheight+25);
    }
    
    if(msie_check()==1)
    {
        $("#header .white-link").css("height","20px")
        $("#header .search-input").css("height","20px")
    }
    
    $( "body" ).click(function( event ) {
        if(!$(event.srcElement).hasClass('noleave'))
            ReturnSearchInput();
    });
});

function msie_check() {
  if (window.navigator.userAgent.match(/Trident/))
     return 1;
  else
     return 0;

}
function ReturnSearchInput() {
    
    $('#search-dropdown').css('display','none');
    $("#search-list").html('');
}
function ShowSearchInput(value) {   
    var baseURL = $('body').data('baseurl');
    $('#btnSearch').removeClass( "icon-search-black" ).addClass( "icon-loading" );
    
    $.ajax({
        url: baseURL+'site/getKeyWord',
        type: "POST",
        data : {LIKEVAL : value},
        success: function(data) {
            if(value===$("#txtSearchValue").val())
            {
                if(data!='')
                {
                    $("#search-list").html(data);               
                    $('#search-dropdown').css('display','block');                
                }
                else
                {
                    ReturnSearchInput();
                }
            }
        },
        complete: function()
        {
            $('#btnSearch').removeClass( "icon-loading" ).addClass( "icon-search-black" );
        }
    }); 
}